package com.atguigu.tingshu.album.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.cache.GuiGuCache;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;

    @Override
    public List<JSONObject> getBaseCategoryList() {

		//1.创建响应结果集合对象-用于封装所有一级分类对象
		List<JSONObject> returnList = new ArrayList<>();
		//2.查询所有分类数据-查询视图即可 共计401条记录
		List<BaseCategoryView> allCategoryList = baseCategoryViewMapper.selectList(null);
		//3.处理一级分类数据
		//3.1 对所有分类集合列表进行分组按照1级分类ID进行分组 得到Map<分组ID，一级分类列表>
		Map<Long, List<BaseCategoryView>> map1 = allCategoryList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		for (Map.Entry<Long, List<BaseCategoryView>> entry1 : map1.entrySet()) {
			//3.2 封装一级分类对象
			JSONObject jsonObject1 = new JSONObject();
			//3.2.1 封装1级分类ID
			Long category1Id = entry1.getKey();
			jsonObject1.put("categoryId",category1Id);
			//3.2.2 封装1级分类名称
			String category1Name = entry1.getValue().get(0).getCategory1Name();
			jsonObject1.put("categoryName",category1Name);

			//4. 处理二级分类数据
			ArrayList<JSONObject> jsonObject2List = new ArrayList<>();
			//4.1 对"1级"分类集合按照二级分类ID进行分组
			Map<Long, List<BaseCategoryView>> map2 = entry1.getValue().stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			for (Map.Entry<Long, List<BaseCategoryView>> entry2 : map2.entrySet()) {
				//4.3 封装二级分类对象
				JSONObject jsonObject2 = new JSONObject();
				//4.3.1 封装2级分类ID
				Long category2Id = entry2.getKey();
				jsonObject2.put("categoryId",category2Id);
				//4.3.2 封装2级分类名称
				String category2Name = entry2.getValue().get(0).getCategory2Name();
				jsonObject2.put("categoryName",category2Name);
				jsonObject2List.add(jsonObject2);
				//5. 处理三级分类数据
				ArrayList<JSONObject> jsonObject3List = new ArrayList<>();
				//5.1 对"2级"分类列表进行遍历
				for (BaseCategoryView baseCategoryView : entry2.getValue()) {
					//5.2 封装三级分类JSONOBject对象
					JSONObject jsonObject3 = new JSONObject();
					//5.2.1 封装3级分类ID
					jsonObject3.put("categoryId", baseCategoryView.getCategory3Id());
					//5.2.2 封装3级分类名称
					jsonObject3.put("categoryName", baseCategoryView.getCategory3Name());
					jsonObject3List.add(jsonObject3);
				}
				//5.4 将3级分类对象集合加入到二级分类对象"categoryChild"属性中
				jsonObject2.put("categoryChild", jsonObject3List);
			}
			//4.5 将二级分类集合封装在一级分类对象中"categoryChild"属性中
			jsonObject1.put("categoryChild", jsonObject2List);
			returnList.add(jsonObject1);
		}
		return returnList;
	}

	@Override
	public List<BaseAttribute> getAttributesByCategory1Id(Long category1Id) {
		return baseAttributeMapper.getAttributesByCategory1Id(category1Id);
	}

	/**
	 * 根据三级分类ID查询分类视图
	 * @param category3Id
	 * @return
	 */
	@GuiGuCache(prefix = "category:categoryview:")
	@Override
	public BaseCategoryView getCategoryView(Long category3Id) {
		return baseCategoryViewMapper.selectById(category3Id);
	}

	/**
	 * 根据1级分类ID查询置顶7个三级分类列表
	 *
	 * @param category1Id
	 * @return
	 */
	@Override
	public List<BaseCategory3> getTop7BaseCategory3(Long category1Id) {
		//1.根据1级分类ID查询包含2级分类ID列表
		LambdaQueryWrapper<BaseCategory2> baseCategory2LambdaQueryWrapper = new LambdaQueryWrapper<>();
		baseCategory2LambdaQueryWrapper.eq(BaseCategory2::getCategory1Id, category1Id);
		baseCategory2LambdaQueryWrapper.select(BaseCategory2::getId);
		List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(baseCategory2LambdaQueryWrapper);
		//2.根据2级分类ID列表查询包含3级分类列表
		if (CollectionUtil.isNotEmpty(baseCategory2List)) {
			List<Long> baseCategory2IdList = baseCategory2List.stream().map(BaseCategory2::getId).collect(Collectors.toList());
			LambdaQueryWrapper<BaseCategory3> baseCategory3LambdaQueryWrapper = new LambdaQueryWrapper<>();
			//2.1 封装查询条件 包含在二级分类ID下的三级分类
			baseCategory3LambdaQueryWrapper.in(BaseCategory3::getCategory2Id, baseCategory2IdList);
			//2.2 查询置顶分类
			baseCategory3LambdaQueryWrapper.eq(BaseCategory3::getIsTop, 1);
			//2.3 根据序号降序排
			baseCategory3LambdaQueryWrapper.orderByAsc(BaseCategory3::getOrderNum);
			//2.4 限制返回记录数：7个
			baseCategory3LambdaQueryWrapper.last("limit 7");
			return baseCategory3Mapper.selectList(baseCategory3LambdaQueryWrapper);
		}
		return null;
	}

	@Override
	public JSONObject getBaseCategory1List(Long category1Id) {
		JSONObject jsonObject1 = new JSONObject();
		LambdaQueryWrapper<BaseCategoryView> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(BaseCategoryView::getCategory1Id,category1Id);
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(queryWrapper);
		if (CollectionUtil.isNotEmpty(baseCategoryViewList)){
			jsonObject1.put("categoryId",category1Id);
			jsonObject1.put("categoryName",baseCategoryViewList.get(0).getCategory1Name());

			List<JSONObject> jsonObject2List = new ArrayList<>();
			Map<Long, List<BaseCategoryView>> map2 = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			for (Map.Entry<Long, List<BaseCategoryView>> entry2 : map2.entrySet()) {
				JSONObject jsonObject2 = new JSONObject();
				jsonObject2.put("categoryId",entry2.getKey());
				jsonObject2.put("categoryName",entry2.getValue().get(0).getCategory2Name());
				jsonObject2List.add(jsonObject2);
				List<JSONObject> jsonObject3List = new ArrayList<>();
				for (BaseCategoryView baseCategoryView : entry2.getValue()) {
					JSONObject jsonObject3 = new JSONObject();
					jsonObject3.put("categoryId",baseCategoryView.getCategory3Id());
					jsonObject3.put("categoryName",baseCategoryView.getCategory3Name());
					jsonObject3List.add(jsonObject3);
				}
				jsonObject2.put("categoryChild",jsonObject3List);
			}
			jsonObject1.put("categoryChild",jsonObject2List);
		}
		return jsonObject1;
	}
}
